﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class Blur : MonoBehaviour
{
    public RenderTexture screenRT;
    public RenderTexture tempRT;
    public Camera m_camera;
    public int blurTimes = 0;
    public Shader blurShader;
    private Material blurMat;
    private CommandBuffer cb;
    private int blurID;

    private void OnEnable()
    {
       
        blurMat = new Material(blurShader);
        cb = new CommandBuffer();
        cb.name = "copyScreenxxxxxx";
        m_camera.AddCommandBuffer(CameraEvent.AfterSkybox, cb);
        blurID = Shader.PropertyToID("_BlurTex");
    }
    public void Update()
    {
        cb.Clear();
        screenRT = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 0);
        tempRT = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 0);
        cb.Blit(BuiltinRenderTextureType.CurrentActive, screenRT);

        for (int i = 0; i < blurTimes; ++i)
        {
            int horizontalPass = blurMat.FindPass("Horizontal");
            int verticalPass = blurMat.FindPass("Vertical");
            cb.Blit(screenRT, tempRT, blurMat, horizontalPass);
            cb.Blit(tempRT, screenRT, blurMat, verticalPass);
        }
        screenRT.SetGlobalShaderProperty("_BlurTex");
    }
}
